package com.hznu.jspFinal.controller;


import com.hznu.jspFinal.entity.Composepaper;
import com.hznu.jspFinal.entity.ExamPaper;
import com.hznu.jspFinal.entity.ExerciseInfo;
import com.hznu.jspFinal.myBeans.ExamPaperBean;
import com.hznu.jspFinal.myBeans.R;
import com.hznu.jspFinal.service.ExamPaperService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * author:wp
 * time : 11:22 2018/06/17
 */

@Api(description = "试卷接口",value = "ExamPaper")
@RestController
@RequestMapping("/ExamPaper")
public class ExamPaperController {

    @Autowired
    private ExamPaperService examPaperService;

    @ApiOperation(value = "创建一张新的试卷")
    @PostMapping("/add")
    public R addOne(@RequestBody ExamPaper examPaper)
    {
        try{
            examPaperService.add(examPaper);
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }

    @ApiOperation(value = "修改试卷基本信息")
    @PostMapping("/update")
    public R update(@RequestBody ExamPaper examPaper)
    {
        try {
            examPaperService.update(examPaper);
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }


    @ApiOperation(value = "通过试卷id删除试卷")
    @GetMapping("/delete/{id}")
    public R delete(@PathVariable Integer id)
    {
        try{
            examPaperService.delete(id);
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }

    @ApiOperation(value = "设置试卷的题型信息")
    @PostMapping("/setExerTypes")
    public R setExerTypes(@RequestBody ExamPaperBean examPaperBean)
    {
        try {
            examPaperService.setExerTypes(examPaperBean.getExamPaper().getId(),examPaperBean.getPaperExerTypeInfos());
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }

    @ApiOperation(value = "增加试卷的题型")
    @PostMapping("/addExerTypes")
    public R addExerTypes(@RequestBody ExamPaperBean examPaperBean)
    {
        try {
            examPaperService.addExerTypes(examPaperBean.getExamPaper().getId(),examPaperBean.getPaperExerTypeInfos());
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }

    @ApiOperation(value = "给试卷添加题目")
    @PostMapping("/addExerToExamPaper")
    public R addExer(@RequestBody Composepaper composepaper)
    {
        try{
            examPaperService.addExer(composepaper);
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }

    @ApiOperation(value = "通过试卷id返回试卷的基本信息、题型、题目集")
    @GetMapping("/findDetailsById/{id}")
    public R findDetails(@PathVariable Integer id)
    {
        try{
            return R.ok().put("data",examPaperService.findExamPaperDetailsById(id));
        } catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }


}
